package com.xty.xtyle.detail;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xty.xtyle.R;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.db.mix.MixCloth;
import com.xty.xtyle.db.mix.MixDao;
import com.xty.xtyle.db.mix.MixType;
import com.xty.xtyle.db.mix.MixTypeDao;
import com.xty.xtyle.event.FreshMixEvent;
import com.xty.xtyle.mix.MixPicActivity;
import com.xty.xtyle.mix.TimePickerDialog;
import com.xty.xtyle.utils.CommonUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static com.xty.xtyle.mix.MixPicActivity.PIC_STATE_POSITION;

public class AddMixActivity extends AppCompatActivity {

    private static final int leftUp = 1;
    private static final int leftDown = 2;
    private static final int rightUp = 3;
    private static final int rightCenter = 4;
    private static final int rightDown = 5;

    ImageView image_up, image_down, pie_up, pie_middle, pie_down;
    Cloth leftUpCloth, leftDownCloth, rightUpCloth, rightMidCloth, rightDownCloth;
    long mixTime;
    EditText edit_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_mix);
        initBar();
    }

    void initBar() {
        findViewById(R.id.left_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView titleView = findViewById(R.id.center_icon);
        titleView.setText("添加搭配");

        image_up = findViewById(R.id.image_up);
        image_down = findViewById(R.id.image_down);
        pie_up = findViewById(R.id.pie_up);
        pie_middle = findViewById(R.id.pie_middle);
        pie_down = findViewById(R.id.pie_down);
        edit_type = findViewById(R.id.edit_type);
        //5张图片的点击时间,左边
        findViewById(R.id.image_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), leftUp);
            }
        });
        findViewById(R.id.image_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), leftDown);
            }
        });
        findViewById(R.id.pie_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), rightUp);
            }
        });
        findViewById(R.id.pie_middle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), rightCenter);
            }
        });
        findViewById(R.id.pie_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MixPicActivity.startActivity((Activity) view.getContext(), rightDown);
            }
        });

        findViewById(R.id.mix_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                save();
            }
        });
    }
    //从衣架挑选回来的内容,通过position来确定是5张图片的内容,id为衣服的ID
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == MixPicActivity.PIC_STATE_BACK) {
            int position = data.getIntExtra(PIC_STATE_POSITION, 0);
            int clothId = data.getIntExtra("id", -1);
            if (position < 1 || clothId < 0) return;
            decode(position, clothId);
        }
    }
    /**
     *
     * @param position 5张图片的位置
     * @param clothId 衣服的id
     */
    void decode(int position, int clothId) {
        ClothDao clothDao = new ClothDao();
        switch (position) {
            case 1:
                leftUpCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(leftUpCloth.getImage()).into(image_up);
                break;
            case 2:
                leftDownCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(leftDownCloth.getImage()).into(image_down);
                break;
            case 3:
                rightUpCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(rightUpCloth.getImage()).into(pie_up);
                break;
            case 4:
                rightMidCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(rightMidCloth.getImage()).into(pie_middle);
                break;
            case 5:
                rightDownCloth = clothDao.findCloth(clothId);
                Glide.with(this).load(rightDownCloth.getImage()).into(pie_down);
                break;
        }
    }

    /**
     * 如果挑选了一件也可以认为是搭配
     */
    public void save() {
        MixCloth mixCloth = new MixCloth();
        boolean load = false;
        if (null != leftUpCloth) {
            load = true;
            mixCloth.setUpId(leftUpCloth.getId());
        }
        if (null != leftDownCloth) {
            load = true;
            mixCloth.setDownId(leftDownCloth.getId());
        }
        if (null != rightUpCloth) {
            load = true;
            mixCloth.setPieUpId(rightUpCloth.getId());
        }
        if (null != rightMidCloth) {
            load = true;
            mixCloth.setPieCenterId(rightMidCloth.getId());
        }
        if (null != rightDownCloth) {
            load = true;
            mixCloth.setPieDownId(rightDownCloth.getId());
        }

        String type = edit_type.getText().toString().trim();
        checkType(type);
        mixCloth.setType(type);

        if (mixTime < 1) {
            showDialog();
            return;
        }
        mixCloth.setTime(""+mixTime);

        if (load) {
           boolean saved =  mixCloth.save();
            if (saved) {
                EventBus.getDefault().post(FreshMixEvent.getInstance(1));
                CommonUtil.showToast("混搭添加成功");
            }
            else CommonUtil.showToast("混搭添加失败");

//            quey(type);
        }


    }

    void quey(String type) {
        MixDao mixDao = new MixDao();
        List<MixCloth> mixes = mixDao.queryAll();
        if (null != mixes) CommonUtil.showToast("mix的总长度"+mixes.size());
        else CommonUtil.showToast("mix的总长度   = 0");

        List<MixCloth> cloths = mixDao.queryAllByType(type);
        if (null != cloths) CommonUtil.showToast(type+"mix的子长度"+mixes.size());
        else CommonUtil.showToast(type+"mix的子长度   = 0");
    }


    TimePickerDialog pickTimeDialog;
    private void showDialog() {
        pickTimeDialog = new TimePickerDialog();
        pickTimeDialog.show(getSupportFragmentManager(), "pick");
        pickTimeDialog.setCallBack(new TimePickerDialog.CallBack() {
            @Override
            public void time(long time) {
                Log.d("FlutterPush", "back  imageFile:"+time);
                if (time > 1) {
                    mixTime = time;
                }
            }
        });
    }

    /**
     * 如果类型没有，则添加
     * @param type
     */
    void checkType(String type) {
        if (!TextUtils.isEmpty(type)) {
            MixTypeDao clothTypesDao = new MixTypeDao();
            MixType types = clothTypesDao.findMixType(type);
            if (null == types) {
                MixType clothTypes = new MixType(type);
                clothTypes.save();
            }
        }
    }
}
